package com.breakfast.fun.ui.web;

import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.breakfast.fun.R;
import com.breakfast.fun.base.BaseActivity;
import com.breakfast.fun.base.BaseModel;
import com.breakfast.fun.base.BasePresenter;
import com.breakfast.fun.data.entity.user.recharge.CmbPay;
import com.breakfast.fun.view.widget.TitleBarView;

import butterknife.Bind;
import cmb.pb.util.CMBKeyboardFunc;

public class CmbPayActivity extends BaseActivity<WebPresenter, WebModel> {

    @Bind(R.id.tbv_cmb_pay)
    TitleBarView tbvCmbPay;
    @Bind(R.id.wv_cmb_pay)
    WebView wvCmbPay;

    private WebViewClient webViewClient = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            CMBKeyboardFunc kbFunc = new CMBKeyboardFunc(CmbPayActivity.this);
            if (kbFunc.HandleUrlCall(wvCmbPay, url) == false) {
                return super.shouldOverrideUrlLoading(view, url);
            } else {
                return true;
            }
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            //页面加载完成了
            //设置页面的标题
            hideLoadingDialog();
        }
    };

    private WebChromeClient webChromeClient = new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {

        }
    };

    @Override
    public int getLayoutId() {
        return R.layout.activity_cmb_pay;
    }

    @Override
    public void initView() {
        tbvCmbPay.setTitleNoRight("一网通支付");
        tbvCmbPay.setBack(this);


        Bundle bundle = getIntent().getExtras();
        CmbPay cmbpay = (CmbPay) bundle.getSerializable("cmbpay");


        wvCmbPay.setWebChromeClient(webChromeClient);
        wvCmbPay.setWebViewClient(webViewClient);

        //允许运行脚本语言
        WebSettings settings = wvCmbPay.getSettings();
        settings.setJavaScriptEnabled(true);

        String BranchID = cmbpay.getBranchID();
        String CoNo = cmbpay.getCoNo();
        String BillNo = cmbpay.getBillNo();
        String Amount = cmbpay.getAmount();
        String Date = cmbpay.getDate();
        String MerchantUrl = cmbpay.getMerchantUrl();
        String MerchantPara = cmbpay.getMerchantPara();
        String MerchantCode = cmbpay.getMerchantCode();

        String url = cmbpay.getPayUrl();
        String postData = "BranchID=" + BranchID +
                "&CoNo=" + CoNo + "&BillNo=" + BillNo + "&Amount=" + Amount +
                "&Date=" + Date +
                "&MerchantUrl=" + MerchantUrl +
                "&MerchantPara=" + MerchantPara +
                "&MerchantCode=" + MerchantCode;

        showLoadingDialog(true);
        wvCmbPay.postUrl(url, postData.getBytes());
    }
}
